package cn.redragon.soa.config;


import cn.redragon.soa.common.web.dao.impl.BaseDaoImpl;
import com.querydsl.jpa.impl.JPAQueryFactory;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(repositoryBaseClass = BaseDaoImpl.class, basePackages = {"cn.redragon.**.dao"})
@EntityScan({"cn.redragon.**.entity"})
@ConditionalOnClass({NamedParameterJdbcTemplate.class, JPAQueryFactory.class})
@ConditionalOnProperty(value = "digi.common.datasource.mode", havingValue = "single", matchIfMissing = true)
public class JpaConfig {

    @Bean
    public JPAQueryFactory createJpaQueryFactoryPrimary(EntityManager entityManager) {
        return new JPAQueryFactory(entityManager);
    }

    @Bean
    public NamedParameterJdbcTemplate createNamedParameterJdbcTemplate(DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }
}